package cn.yuhao.party.bar.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.itcast.commons.CommonUtils;
import cn.itcast.servlet.BaseServlet;
import cn.yuhao.party.bar.dao.BaroutDao;
import cn.yuhao.party.bar.domain.Bar;
import cn.yuhao.party.pager.PageBean;

public class BarOutServlet extends BaseServlet {

	private BaroutDao ns = new BaroutDao();

	public String editBar(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Bar Bar = CommonUtils.toBean(request.getParameterMap(), Bar.class);
		ns.editBar(Bar);
		request.setAttribute("msg", "修改校内导航条成功");
		return "f:/adminjsps/msg.jsp";
	}
	public String reload(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String gtheme = request.getParameter("id");
		Bar Bar = ns.findById(gtheme);
		request.setAttribute("Bar", Bar);
		return "f:/adminjsps/bar/editout.jsp";
	}

	public String delete(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String gtheme = request.getParameter("id");
		ns.delete(gtheme);
		request.setAttribute("msg", "恭喜您校内活动删除成功");
		return "f:/adminjsps/msg.jsp";
	}

	public String addBar(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Bar Bar = CommonUtils.toBean(request.getParameterMap(), Bar.class);
		Bar.setId(CommonUtils.uuid());
		ns.addBar(Bar);
		request.setAttribute("msg", "恭喜您校外活动添加成功");
		return "f:/adminjsps/msg.jsp";

	}

	

	public String findAll(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int pc = getPc(request);
		String url = getUrl(request);
		PageBean<Bar> pb = (PageBean<Bar>) ns.findAll(pc);

		pb.setUrl(url);
		request.setAttribute("msg", "校内活动");
		request.setAttribute("pb", pb);
		return "f:/adminjsps/bar/listout.jsp";
	}

	public String findById(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String gtheme = request.getParameter("gtheme");
		Bar Bar = ns.findById(gtheme);
		request.setAttribute("Bar", Bar);
		return "f:/jsps/pingtai/Barcon.jsp";
	}

	private static String getSub(String str) {
		if (str == null || str.equals(""))
			return " ";
		if (str.length() > 100)
			str = str.substring(0, 100) + "...";
		return str;
	}

	public static String getStringDate() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date = formatter.format(currentTime);
		return date;
	}


	private int getPc(HttpServletRequest req) {
		int pc = 1;
		String param = req.getParameter("pc");
		if (param != null && !param.trim().isEmpty()) {
			try {
				pc = Integer.parseInt(param);
			} catch (RuntimeException e) {
			}
		}
		return pc;
	}

	

	private String getUrl(HttpServletRequest req) {
		String url = req.getRequestURI() + "?" + req.getQueryString();

		int index = url.lastIndexOf("&pc=");
		if (index != -1) {
			url = url.substring(0, index);
		}
		return url;
	}

}
